package com.scalar.chiptrack.operations.dataentry.inventoryTransit.actioncommands;

import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.dataentry.inventoryTransit.forms.InventoryTransitForm;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.release.dao.RelaseDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: ChandraSekhar
 * Date: Fed 06, 2008
 * Time: 12:28:08 PM
 */
public class InventoryTransitLoadLotNumbersCommand extends ActionCommand
{

    private static final String ACTION_NAME = "/inventoryTransit.do" ;


    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {
        InventoryTransitForm inventoryTransitForm = (InventoryTransitForm) form;

        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);
        String locationType = null;
        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        ArrayList allRunningWipLotList;
        LotDetails  lotDetails = new LotDetails();

        try
        {
            if ( LOCATION_TYPE_ASSEMBLY.equalsIgnoreCase(inventoryTransitForm.getProcessType()) )
            {
                locationType = LOCATION_TYPE_ASSEMBLY;
            }
            else if ( LOCATION_TYPE_FT.equalsIgnoreCase(inventoryTransitForm.getProcessType()) )
            {
                locationType = LOCATION_TYPE_TEST;
            }
            else if ( LOCATION_TYPE_FPO.equalsIgnoreCase(inventoryTransitForm.getProcessType()) )
            {
                locationType = LOCATION_TYPE_TEST;
            }
            else if ( LOCATION_TYPE_TAPE_AND_REEL.equalsIgnoreCase(inventoryTransitForm.getProcessType()) )
            {
                locationType = LOCATION_TYPE_CLASS_STORE;
            }

            allRunningWipLotList = DataEntryDAO.getAllRunningLots( inventoryTransitForm.getProcessType() , inventoryTransitForm.getProcessStepId() , inventoryTransitForm.getLocationId(), 0,false  );

            if ( allRunningWipLotList == null || allRunningWipLotList.size() == 0 )
            {
                info.put("No WIP lots available" , null );
            }

            inventoryTransitForm.setWipLotsList( allRunningWipLotList );

            request.setAttribute("nextLocations",  RelaseDAO.loadProcessVendors( locationType ) );
            request.setAttribute("processTypeList", lotDetails.getInventoryTransitProcessList() );
            request.setAttribute("reciveProcessList", lotDetails.getInventoryReceiveProcessList() );
            request.setAttribute("partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( inventoryTransitForm.getProcessType() ) );
            request.setAttribute("locationList", VendorDAO.getInventoryLocationsByProcessByUserType( null, locationType, user.getVendorId() ) );
            request.setAttribute("action", action);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return mapping.findForward("inventoryTransit");
    }
}